package yxy.game.pm2.command.camptower;

import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.racenexus;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Constant;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.CurrencyNotEnoughException;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.CampTowerMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.battle.BattleHandle;
import yxy.game.pm2.module.camptower.FourSealModule;
import yxy.game.pm2.module.playtype.PlayTypeHandle;
import yxy.game.pm2.module.playtype.PlayTypeModule;

import java.util.Map;

/**
 * 阵营塔 - 四象归一提升
 *
 * @ClassName CMD_FourSealUpgrade
 * @Author jingtao-zheng
 * @Date 2022/8/9 14:10
 * @Version 1.0
 **/
@MessageType(proto = Command.FourSealUpgrade, classtype = CampTowerMessage.FourSealUpgradeRequest.class)
public class CMD_FourSealUpgrade extends Command<CampTowerMessage.FourSealUpgradeRequest> {
    @Override
    public void execute(MessageSource messageSource, CampTowerMessage.FourSealUpgradeRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int type = request.getType().getNumber();
        CampTowerMessage.FourSealUpgradeResponse.Builder response = CampTowerMessage.FourSealUpgradeResponse.newBuilder();
        response.setType(request.getType());

        FourSealModule module = FourSealModule.get(playerCode);
        if (module == null) {
            send(messageSource, response, Result.DataError);
            return;
        }
        //当前等级
        Integer lv = module.getFourSeal().get(type);
        //计算id
        int racenexusId = matchRacenexusId(type);
        int id = Constant.MAX_LV * (racenexusId - 1) + (lv + 1);

        racenexus rs = DataCenter.getData(id, racenexus.class);
        //花费
        Currency currency = Currency.create(rs.getCosttype(), rs.getCostid(), rs.getCostnum());
        if (currency == null || rs.getCostnum() == 0) {
            send(messageSource, response, Result.GuardCantOperate);
        }
        try {
            new CurrencyHandle().cost(playerCode, CCReason.阵营塔, currency);
        } catch (CurrencyNotEnoughException e) {
            send(messageSource, response, new Result(Result.CostFail, e.getType().getNumber(), e.getDataId()));// 消费不足
            return;
        }
        //加等级
        module.getFourSeal().put(type, lv + 1);
        module.set();

        //战力
        PlayTypeModule playTypeModule = PlayTypeModule.get(playerCode);
        PlayTypeHandle.saveFourSeal(playerCode, playTypeModule);//提前把战力算到中间缓存

        new BattleHandle().powerUpdate(playerCode);// 战力更新


        for (Map.Entry<Integer, Integer> integerIntegerEntry : module.getFourSeal().entrySet()) {
            CampTowerMessage.FourSeal.Builder fourSealResponse = CampTowerMessage.FourSeal.newBuilder();
            CampTowerMessage.LineupType lineupType = matchLineupType(integerIntegerEntry.getKey());
            fourSealResponse.setType(lineupType);
            fourSealResponse.setLv(integerIntegerEntry.getValue());
            response.addFourSealDatas(fourSealResponse);
        }
        send(messageSource, response, Result.Success);
    }

    private CampTowerMessage.LineupType matchLineupType(int type) {
        if (type == Constant.WEI) {
            return CampTowerMessage.LineupType.valueOf(Constant.WEI);
        }
        if (type == Constant.SHU) {
            return CampTowerMessage.LineupType.valueOf(Constant.SHU);
        }
        if (type == Constant.WU) {
            return CampTowerMessage.LineupType.valueOf(Constant.WU);
        }
        if (type == Constant.QUN) {
            return CampTowerMessage.LineupType.valueOf(Constant.QUN);
        }
        return null;
    }

    private int matchRacenexusId(int type) {
        if (type == Constant.WEI) {
            return Constant.WEI_RACENEXUS_ID;
        }
        if (type == Constant.SHU) {
            return Constant.SHU_RACENEXUS_ID;
        }
        if (type == Constant.WU) {
            return Constant.WU_RACENEXUS_ID;
        }
        if (type == Constant.QUN) {
            return Constant.QUN_RACENEXUS_ID;
        }
        return 0;
    }

    private void send(MessageSource messageSource, CampTowerMessage.FourSealUpgradeResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }
}
